/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ads.services;

import ads.list.ListEmptyException;
import ads.list.SimpleListI;
import ads.list.SimpleListImpl;
import ch.zhaw.ads.CommandExecuter;

/**
 *
 * @author framet
 */
public class SimpleListService implements CommandExecuter {

    SimpleListI simpleList;

    @Override
    public String execute(String command) throws Exception {
        if (simpleList == null) {
            simpleList = new SimpleListImpl();
        }
        if (command.matches("\\w*:(\\d+:?)*")) {
            switch (command.split(":")[0]) {
                case "first":
                    return getFirst();
                case "last":
                    return getLast();
                case "nth":
                    if (command.matches("nth:\\d+")) {
                        return getNth(Integer.valueOf(command.split(":")[1]));
                    } else {
                        return "Error:101 - Format does not match (nth:xxx)";
                    }
                case "removefirst":
                    return removeFirst();
                case "removelast":
                    return removeLast();
                case "addfirst":
                    if (command.matches("addfirst:\\d+")) {
                        addFirst(Integer.valueOf(command.split(":")[1]));
                        return "Msg:201 - Added.";
                    } else {
                        return "Error:101 - Format does not match (addfirst:xxx)";
                    }
                case "addlast":
                    if (command.matches("addlast:\\d+")) {
                        addLast(Integer.valueOf(command.split(":")[1]));
                        return "Msg:201 - Added.";
                    } else {
                        return "Error:101 - Format does not match (addlast:xxx)";
                    }
                case "addnth":
                    if (command.matches("addnth:\\d+:\\d+")) {
                        addNth(Integer.valueOf(command.split(":")[1]), Integer.valueOf(command.split(":")[2]));
                        return "Msg:201 - Added.";
                    } else {
                        return "Error:101 - Format does not match (addlast:xxx:xxx)";
                    }
            }

        }
        return "Command not recognized";
    }

    public void addFirst(Object obj) {
        simpleList.addFirst(obj);
    }

    public String getFirst() throws ListEmptyException {
        return String.valueOf(simpleList.getFirst());
    }

    public String removeFirst() throws ListEmptyException {
        return String.valueOf(simpleList.removeFirst());
    }

    public void addLast(Object obj) {
        simpleList.addLast(obj);
    }

    public String getLast() throws ListEmptyException {
        return String.valueOf(simpleList.getLast());
    }

    public String removeLast() throws ListEmptyException {
        return String.valueOf(simpleList.removeLast());
    }

    public void addNth(Object obj, int index) {
        simpleList.addNth(obj, index);
    }

    public String getNth(int index) throws ListEmptyException {
        return String.valueOf(simpleList.getNth(index));
    }
}
